<?php
/*
 * @Author: 莫莫
 * @Phone: 13450159681
 * @Description: 
 * @Date: 2024-08-22 11:51:52
 * @LastEditTime: 2025-04-14 17:35:36
 */

namespace app\api\controller\wanlshop;

use app\common\controller\Api;
use app\common\library\Api as ApiLib;
use think\Db;

/**
 * 远程接口
 */
class Statistics extends Api
{
	protected $noNeedLogin = [''];
	protected $noNeedRight = ['*'];


	public function userOrder()
	{
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		if ($this->request->isGet()) {
			$userType = $this->request->get('userType') ?? 1;
			$limit = $this->request->get('limit') ?? 10;
			$page = $this->request->get('page') ?? 1;
			$userID = $this->request->get('userID') ?? null;
			$api = new ApiLib();
			$data = $api->statUserOrder($userType, $userID, $limit, $page);
			$this->success('返回成功', $data['data']);
		}
		$this->error(__('非法请求'));
	}

	public function userOneOrder()
	{
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		if ($this->request->isGet()) {
			$userType = $this->request->get('userType') ?? 1;
			$limit = $this->request->get('limit') ?? 10;
			$page = $this->request->get('page') ?? 1;
			$userID = $this->request->get('userID') ?? null;
			$api = new ApiLib();
			$data = $api->statOneUserOrder($userType, $userID, $limit, $page);
			$arr[] = $data['data'];
			$this->success('返回成功', ['data' => $arr]);
		}
		$this->error(__('非法请求'));
	}

	public function userCount()
	{
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		if ($this->request->isGet()) {
			$is_all = $this->request->get('is_all') ?? 0;
			$api = new ApiLib();
			$res = $api->statUserCount();
			$data = ['agent_count' => $res['data']['agent_count'], 'store_count' => $res['data']['store_count']];

			$total_score = Db::connect(config("oubi_config"))->name('mc_members')->sum('credit1'); //获取总积分
			$data['total_score'] = $total_score;
			$where = [];
			$where1 = [];
			if ($is_all == 0) {
				// 本月时间
				$where['created_at'] = array(
					array('egt', strtotime(date('Y-m', time()))),
					array('lt', strtotime(date('Y-m', time()) . '+1 month'))
				);
			}
			$score = Db::connect(config("oubi_config"))->name('yz_point_log')->where($where)->sum('after_point');
			$data['score'] = $score;
			$where1['createtime'] = array(
				array('egt', strtotime(date('Y-m', time()))),
				array('lt', strtotime(date('Y-m', time()) . '+1 month'))
			);
			$members = Db::connect(config("oubi_config"))->name('mc_members')->where($where1)->count();
			$data['members_num'] = $members;
			$data['slideshow'] = model('\app\common\model\Attachment')->where('category', 'slideshow')->column('url');
			foreach ($data['slideshow'] as $key => &$val) {
				$val = cdnurl($val);
			}
			unset($val);
			$this->success('返回成功', $data);
		}
		$this->error(__('非法请求'));
	}

	public function score()
	{
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		if ($this->request->isGet()) {
			$data = Db::connect(config("oubi_config"))->name('mc_members')->field('uid, mobile, credit1, realname,nickname,avatar')
				->order('credit1 desc,uid desc')
				->paginate()->each(function ($val, $key) {
					$components = parse_url($val['avatar']);
					if (!isset($components['scheme'])) {
						$val['avatar'] = 'https://sj.gdobee.top/' . $val['avatar'];
					}
					return $val;
				}); //获取总积分
			$this->success('返回成功', $data);
		}
		$this->error(__('非法请求'));
	}

	public function scoreCount()
	{
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		if ($this->request->isGet()) {
			$start_time = $this->request->get('start_time');
			$end_time = $this->request->get('end_time');
			$type = $this->request->get('type') ?? 'day';
			if (!empty($start_time)) {

				$start = strtotime($start_time);

				$end   = strtotime($end_time);
			} else {

				switch ($type) {

					case 'day':

						$end = strtotime(date('Y-m-d', time()));

						$start = $end - 86400 * 14;

						break;

					case 'month':

						$end = strtotime(date('Y-m', time()));

						$start = $end - 86400 * 30 * 11;

						break;

					case 'year':
						$end = strtotime(date('Y', time()));
						$start = $end - 86400 * 30 * 12 * 5;
						break;

					default:

						$end = strtotime(date('Y-m-d', time()));

						$start = $end - 86400 * 14;
				}
			}

			$i = 0;
			$arr = [];
			while ($start <= $end) {
				$where = [];
				$arr[$i]['time'] = $start;

				switch ($type) {

					case 'day':
						$time_text = date('Y-m-d', $start);
						$arr[$i]['time_text'] = $time_text;
						$after_point = Db::connect(config("oubi_config"))->name('yz_point_log')->where(['point_income_type' => 1])->where('FROM_UNIXTIME(created_at,"%Y-%m-%d")="' . $time_text . '"')->sum('after_point');
						$arr[$i]['after_point']  = round($after_point, 2);
						$start += 86400;
						break;

					case 'month':
						$time_text = date('Y-m', $start);
						$arr[$i]['time_text'] = $time_text;
						$after_point = Db::connect(config("oubi_config"))->name('yz_point_log')->where(['point_income_type' => 1])->where('FROM_UNIXTIME(created_at,"%Y-%m")="' . $time_text . '"')->sum('after_point');
						$arr[$i]['after_point']  = round($after_point, 2);
						$start += 86400 * 30;
						break;

					case 'year':
						$time_text = date('Y', $start);
						$arr[$i]['time_text'] = $time_text;
						$after_point = Db::connect(config("oubi_config"))->name('yz_point_log')->where(['point_income_type' => 1])->where('FROM_UNIXTIME(created_at,"%Y")="' . $time_text . '"')->sum('after_point');
						$arr[$i]['after_point']  = round($after_point, 2);
						$start += 86400 * 30 * 12;
						break;

					default:
						$time_text = date('Y-m-d', $start);
						$arr[$i]['time_text'] = $time_text;
						$after_point = Db::connect(config("oubi_config"))->name('yz_point_log')->where(['point_income_type' => 1])->where('FROM_UNIXTIME(created_at,"%Y-%m-%d")="' . $time_text . '"')->sum('after_point');
						$arr[$i]['after_point']  = round($after_point, 2);
						$start += 86400;
						break;
				}


				$i++;
			}
			$data['after_point'] = array_column($arr, 'after_point');
			$data['time'] = array_column($arr, 'time_text');
			$data['list'] = $arr;
			$this->success('返回成功', $data);
		}
		$this->error(__('非法请求'));
	}

	/**
	 * 获取内容详情
	 *
	 * @ApiSummary  (WanlShop 获取内容详情)
	 * @ApiMethod   (POST)
	 * 
	 * @param string $id 文章ID
	 */
	public function details()
	{
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		$id = $this->request->get('id');
		$id ? $id : ($this->error(__('Invalid parameters')));
		$row = model('app\api\model\wanlshop\Article')
			->where(['id' => $id])
			->find();
		// 1.0.5升级
		if (!$row) {
			$this->error(__('没有找到任何内容'));
		}
		// 点击 +1
		$row->setInc('views');
		$this->success('返回成功', $row);
	}
}
